package com.windmill.core;

import iqq.im.QQActionListener;
import iqq.im.QQException;
import iqq.im.actor.ThreadActorDispatcher;
import iqq.im.bean.QQAccount;
import iqq.im.bean.QQGroup;
import iqq.im.bean.QQMsg;
import iqq.im.bean.QQStatus;
import iqq.im.bean.content.FontItem;
import iqq.im.bean.content.TextItem;
import iqq.im.event.QQActionEvent;
import iqq.im.event.QQActionEvent.Type;
import iqq.im.event.QQNotifyEvent;
import iqq.im.event.QQNotifyEventArgs;
import iqq.im.event.QQNotifyHandler;
import iqq.im.event.QQNotifyHandlerProxy;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

import javax.swing.ImageIcon;
import javax.swing.JOptionPane;

import com.windmill.cmd.RobotReplyDispatcher;
import com.windmill.ui.LoginFrame;
import com.windmill.ui.MainFrame;

public class WindmillClient  {
	private MargicCardClient client;
	private LoginFrame loginFrame;
	private MainFrame mainFrame;
	private RobotReplyDispatcher robot; 
	public void init(LoginFrame loginFrame, MainFrame mainFrame) {
		this.loginFrame = loginFrame;
		this.mainFrame = mainFrame;
		loginFrame.display();
	}	
	
	
	/**
	 * 登录入口
	 */
	public void login(QQAccount account) {
		this.client = new MargicCardClient(account.getUsername(),account.getPassword(), 
							  		new QQNotifyHandlerProxy(this), new ThreadActorDispatcher());
		//机器人处理回复消息线程
		this.robot = new RobotReplyDispatcher(client,new ActionListener() {			
			public void actionPerformed(ActionEvent e) {
				showMsg(e.getActionCommand(),(Color) e.getSource());
			}
		});
		final QQActionListener listener = new QQActionListener() {
			public void onActionEvent(QQActionEvent event) {
				System.out.println("LOGIN_STATUS:" + event.getType());
				if (event.getType() == Type.EVT_OK) {//到这里就算是登录成功了
					client.getGroupList(null);	
					client.beginPollMsg();
					loginFrame.setVisible(false);
					mainFrame.setVisible(true);				
				}
			}
		};
		client.login(QQStatus.ONLINE, listener);	
	}

	/**
	 * GUI发送消息
	 */
	public void sendMsg(String text) {
		QQMsg sendMsg = new QQMsg();
		sendMsg.setType(QQMsg.Type.GROUP_MSG); 
		sendMsg.addContentItem(new TextItem(text));     
		sendMsg.addContentItem(new FontItem());  
		for(QQGroup group:client.getGroupList()){
			sendMsg.setGroup(group);
			client.sendMsg(sendMsg, null);
		}
	}
	/**
	 *GUI显示消息
	 */
	public void showMsg(String msg,Color color){
		this.mainFrame.showMsg(msg,color);
	}

	/**
	 * 被踢下线通知
	 */
	@QQNotifyHandler(QQNotifyEvent.Type.KICK_OFFLINE)
	private void processKickOff(QQNotifyEvent event){
		System.out.println("被踢下线: " + (String)event.getTarget());
		showMsg("被踢下线: " + (String)event.getTarget(), Color.RED);
	}
	
	/**
	 * 需要验证码通知
	 */
	@QQNotifyHandler(QQNotifyEvent.Type.CAPACHA_VERIFY)
	private void processVerify(QQNotifyEvent event) throws IOException{
		QQNotifyEventArgs.ImageVerify verify = (QQNotifyEventArgs.ImageVerify) event.getTarget();
		System.out.print("请输入在项目根目录下verify.png图片里面的验证码:");
		String code = JOptionPane.showInputDialog(new ImageIcon(verify.image),"请输入验证码");
		if(code!=null){
			client.submitVerify(code, event);			
		}
	}
	/**
	 * 聊天消息通知，使用这个注解可以收到QQ消息
	 */
	@QQNotifyHandler(QQNotifyEvent.Type.CHAT_MSG)
	public void processBuddyMsg(QQNotifyEvent event) throws QQException{
		final QQMsg msg = (QQMsg) event.getTarget();
		robot.pushMsg(msg);
	}


	/**
	 * 退出前保存东西并销毁
	 */
	public void exit() {
		client.getDataStore().storeDataStore();
		client.destroy();
		System.exit(0);	
	}
}
